const app = getApp()
const { dataByIdApi, editApi } = require('../../../api/services/yhHidden/index')
const util = require('../../../utils/util.js')
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    id: '',
    info: {},
    list: [],
    isOkList: false,
    tellError: '',
    submitBtnFlag: true,
    editAdmUrl: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`
      }
      if (type === 'month') {
        return `${value}月`
      }
      return value
    },
    show: false,
  },

  // 添加排查项
  handlePCX() {
    let troubleId = this.data.info.id
    wx.navigateTo({
      url: '/pages/admin/yhpcDescAdd/yhpcDescAdd?troubleId=' + troubleId,
    })
  },

  //  签名跳转
  signatureFn() {
    wx.navigateTo({
      url: '/pages/safety/signature/signature?state=comUrl',
    })
  },
  //  排查人签名
  signatureFn1() {
    wx.navigateTo({
      url: '/pages/safety/signature/signature?state=checkUrl',
    })
  },

  // 获取详情
  async getDescData() {
    let params = { id: this.data.id }
    const res = await dataByIdApi(params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    this.setData({ info: res.data.info, list: res.data.list })
    this.getListOk()
    wx.setStorageSync('isKonw', this.data.info.isKnow)
  },

  // 时间
  handleConfirm(event) {
    this.setData({
      'info.limitDate': util.formatTime(new Date(event.detail)),
      show: false,
    })
  },

  showPopup() {
    this.setData({ show: true })
  },

  // 时间弹窗
  onCloseTime() {
    this.setData({ show: false })
  },

  // 排查人
  onChangeCheckName(event) {
    this.setData({ 'info.checkName': event.detail })
  },

  // 现场陪同人
  onChangeEscortName(event) {
    this.setData({ 'info.escortName': event.detail })
  },

  // 现场陪同人电话
  onChangeEscortTel(event) {
    this.setData({ 'info.escortTel': event.detail })
  },

  // 手机号码效验
  validateTell(event) {
    const reg = /^[1][3,4,5,7,8][0-9]{9}$/
    let tell = event.detail.value
    if (reg.test(tell)) {
      this.setData({
        tellError: '',
        submitBtnFlag: true,
      })
    } else {
      this.setData({
        tellError: '手机号码格式错误',
        submitBtnFlag: false,
      })
    }
  },

  // 本次服务内容
  onChangeContent(event) {
    this.setData({ 'info.content': event.detail })
  },

  // 修改隐患is0k=0 草稿状态可修改
  async handleEditYh() {
    const res = await editApi(this.data.info)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    this.getDescData()

    wx.navigateBack({
      delta: 1, //返回上一级页面
    })
  },

  // 排查项问题点
  handlePoint(event) {
    let { item } = event.currentTarget.dataset

    let itemStr = JSON.stringify(item)
    if (this.data.info.isOk != 0) {
      // 整改完成
      wx.navigateTo({ url: '/pages/common/point/point?item=' + itemStr })
    } else {
      //草稿状态
      // 可编辑修改问题点
      wx.navigateTo({ url: '/pages/admin/troubleSpots/troubleSpots?item=' + itemStr })
    }
  },

  handleAffirm() {
    let _this = this
    Dialog.confirm({
      message: '请确认是否已整改',
    })
      .then(async () => {
        console.log(this.data.id)

        const params = 'id=' + _this.data.id
        wx.request({
          url: app.globalData.host + 'scy/app/spTrouble/finished',
          method: 'POST',
          data: params, //参数为键值对字符串
          header: {
            //设置参数内容类型为x-www-form-urlencoded
            'content-type': 'application/x-www-form-urlencoded',
            Accept: 'application/json',
            Authorization: 'Bearer ' + wx.getStorageSync('token'),
          },
          success: function (res) {
            app.toast('整改完成！')
            _this.getDescData()
          },
          error: function (err) {
            let toast = res.data.msg + ''
            app.toast(toast)
          },
        })
      })
      .catch(() => {
        app.toast('已取消')
      })
  },

  //获取排查单是否都提交整改未确认
  getListOk() {
    const isOkList = this.data.list.every(item => item.isOk == 2)

    console.log(isOkList)

    this.setData({ isOkList: isOkList })
  },

  onLoad: function (options) {
    console.log('编辑')
    this.setData({ id: options.id, statusForm: true })
    // this.getDescData()
  },

  onShow: function () {
    if (!this.data.editAdmUrl) {
      this.getDescData()
    }
  },
})
